package com.youku.util;

import static org.junit.Assert.assertNotNull;

import org.apache.commons.lang3.StringUtils;

public class StringHelper {

	public static String suffix(String s) {
		assertNotNull("字符串不能为空", s);

		int index = s.lastIndexOf('.');

		if (index == -1)
			return "";

		return s.substring(index + 1);
	}

	public static String firstWord(String s) {
		s = StringUtils.trim(s);
		if (StringUtils.length(s) == 0)
			return null;

		int index = -1;
		for (int i = 0; i < s.length(); i++) {
			if (!isIdentifier(s.charAt(i))) {
				index = i;
				break;
			}
		}

		return s.substring(0, index);
	}

	public static boolean isIdentifier(char ch) {
		return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
	}

	public static void deleteTokenIfEndWith(StringBuilder errorMsg, String string) {
		if(errorMsg.toString().endsWith("/")){
			errorMsg.deleteCharAt(errorMsg.length()-1);
		}		
	}

}
